<?php
/**
 * 站点添加管理
 */
namespace app\admins\controller\site;
use \app\admins\controller\Auth;
class Site extends Auth{
    
    
    
    /**
     * 开通的站点列表
     */
    public function index(){        
        $name    = input('name');
        $status  = input('status');
        $where = [
            'page'      => input('page'),
            'limit'     => 20,
            'name'      => $name,
            'status'    => $status
        ];
        
        $SiteList = new \logic\site\SiteList();
        $data = $SiteList->getAdminList($where);
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    
    /**
     * 添加一个新的站点
     */
    public function add(){
        try{        
            if(request()->isPost()){
                $site = [
                    'name'          => input('name'),
                    'contactname'   => input('contactname'),
                    'contacttel'    => input('contacttel'),
                    'domaintype'    => input('domaintype'),
                    'domain'        => input('domain'),
                    'logo'          => input('logo'),
                    'provinceid'    => input('code1',0),
                    'cityid'        => input('code2',0),
                    'areaid'        => input('code3',0),
                    'streetid'      => input('code4',0),
                    'addressdetail' => input('addressdetail'),                
                ];
                $admin = [
                    'account'       => input('account'),
                    'password'      => input('password'),
                    'confirmpwd'    => input('confirmpwd'),
                    'truename'      => input('truename'),
                    'gender'        => input('gender'),
                    'mobile'        => input('mobile'),
                    'email'         => input('email'),
                ];
                $SiteManage =  new \logic\site\SiteMainManage($this->getUser());
                $siteId = $SiteManage->adminAdd($site,$admin);
                return $this->success('添加成功','/site/site/index',['id'=>$siteid]);
            }
            $this->assign(get_defined_vars());
            return $this->fetch();
        }catch(\think\Exception $e){
            pr($e);exit;
            $this->error($e->getMessage());
        }
    }
    
    /**
     * 更新状态
     */
    public function editstatus(){
        try{
            $id = input('siteid');
            $SiteManage = new \logic\site\SiteManage();
            $result = $SiteManage->editStatus($id);
            return $this->success('成功');
        }catch(Exception $e){
            return $this->error('失败');
        }
    }
    
    /**
     * 编辑站点
     */
    public function edit(){
        try{
            $domain = config('url.domain');
            $siteid = input('siteid');
            if(request()->isPost()){
                $site = [
                    'name'          => input('name'),
                    'contactname'   => input('contactname'),
                    'contacttel'    => input('contacttel'),
                    'domaintype'    => input('domaintype'),
                    'domain'        => input('domain'),
                    'logo'          => input('logo'),
                    'provinceid'    => input('code1',0),
                    'cityid'        => input('code2',0),
                    'areaid'        => input('code3',0),
                    'streetid'      => input('code4',0),
                    'addressdetail' => input('addressdetail'),                
                ];
                $SiteManage =  new \logic\site\SiteManage($this->getUser());
                $siteId = $SiteManage->adminEdit($siteid,$site);
                return $this->success('修改成功','/site/site/index');
            }            
            $SiteManage = new \logic\site\SiteManage();
            $site = $SiteManage->find(['siteid'=>$siteid]);     
            $this->assign(get_defined_vars());
            return $this->fetch();            
        }catch(\think\Exception $e){
            pr($e);exit;
            return $this->error($e->getMessage());
        }
        
        
    }
    
}